<?php

require_once(WSB_MODULES_PATH .'sherpa/models/BaseGateway.php');
require_once(WSB_MODULES_PATH .'sherpa/models/Addresses.php');

class Sherpa_Model_AddressGateway extends Sherpa_Model_BaseGateway
{

	/**
	 * @return Sherpa_Model_Addresses
	 */
	public function fetchAddresses()
	{
		$auth = WSB_Auth_Frontend::getInstance();
		if($auth->hasIdentity()) {
			$feuser = $auth->getIdentity();
			$select = $this->getDbSelect();
			$select->from(array('b' => 'sherpa_addresses'));
			$select->where('feuserId=?', $feuser->id);
			return new Sherpa_Model_Addresses($select->query()->fetchAll());
		} else {
			return new Sherpa_Model_Addresses(array());
		}
	}

	/**
	 * @param integer $addressId
	 * @return Sherpa_Model_Address
	 */
	public function fetchAddress($addressId)
	{
		$select = $this->getDbSelect();
		$select->from(array('b' => 'sherpa_addresses'));
		$select->where('id=?', $addressId);
		$row = $select->query()->fetch();
		return new Sherpa_Model_Address($row);
	}

	/**
	 * @param array $address
	 * @return integer Last insert ID
	 */
	public function addAddress(array $address)
	{
		$auth = WSB_Auth_Frontend::getInstance();
		if($auth->hasIdentity()) {
			$feuser = $auth->getIdentity();
			$data = array(
				'feuserId' => $feuser->id,
				'firstName' => $address['firstName'],
				'lastName' => $address['lastName'],
				'company' => $address['company'],
				'street' => $address['street'],
				'poBox' => $address['poBox'],
				'zipCode' => $address['zipCode'],
				'city' => $address['city'],
				'country' => $address['country'],
				'phone' => $address['phone'],
				'email' => $address['email'],
				'modified' => new Zend_Db_Expr('NOW()'),
				'created' => new Zend_Db_Expr('NOW()')
			);
			$db = $this->getDb();
			$db->insert('sherpa_addresses', $data);
			return $db->lastInsertId('sherpa_addresses');
		} else {
			return 0;
		}
	}

	/**
	 * @param array $address
	 * @param integer $id
	 * @return integer Affected rows
	 */
	public function updateAddress(array $address, $id)
	{
		$id = intval($id);
		$auth = WSB_Auth_Frontend::getInstance();
		if($auth->hasIdentity() && ($id > 0)) {
			$feuser = $auth->getIdentity();
			$data = array(
				'firstName' => $address['firstName'],
				'lastName' => $address['lastName'],
				'company' => $address['company'],
				'street' => $address['street'],
				'poBox' => $address['poBox'],
				'zipCode' => $address['zipCode'],
				'city' => $address['city'],
				'country' => $address['country'],
				'phone' => $address['phone'],
				'email' => $address['email'],
				'modified' => new Zend_Db_Expr('NOW()')
			);
			$where = 'id=' . $id . ' AND feuserId=' . $feuser->id;
			return $this->getDb()->update('sherpa_addresses', $data, $where);
		} else {
			return 0;
		}
	}

	/**
	 * @param integer $id
	 * @return integer Affected rows
	 */
	public function deleteAddress($id)
	{
		$id = intval($id);
		$auth = WSB_Auth_Frontend::getInstance();
		if($auth->hasIdentity() && ($id > 0)) {
			$feuser = $auth->getIdentity();
			$where = 'id=' . $id . ' AND feuserId=' . $feuser->id;
			return $this->getDb()->delete('sherpa_addresses', $where);
		} else {
			return 0;
		}
	}

}
